//
//  WholeFreedomViewController.m
//  Qyer
//
//  Created by 赵宇 on 15/9/18.
//  Copyright (c) 2015年 赵宇. All rights reserved.
//

#import "WholeFreedomViewController.h"
#import "BaseHeader.h"
@interface WholeFreedomViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UISegmentedControl *segment;
}
@end

@implementation WholeFreedomViewController
-(void)dealloc
{
    [_table release];
    [_urlStr release];
    [_finalUrl release];
    [_inforArr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = YES;
    _inforArr = [[NSMutableArray alloc]init];
    self.myPage = 1;
    [self creatNavigation];
    [self creatSegment];
    if (self.left) {
        segment.selectedSegmentIndex = 0;
        [self getTheData:0 page:1 isUp:NO];
        
    }else{
        segment.selectedSegmentIndex = 1;
        [self getTheData:1 page:1 isUp:NO];
    }
}
-(void)creatNavigation
{
    UIView *navViea = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    navViea.userInteractionEnabled  = YES;
    navViea.backgroundColor = [UIColor colorWithRed:252./255 green:252./255 blue:252./255 alpha:1];
    UIButton *backBut = [UIButton buttonWithType:UIButtonTypeCustom];
    backBut.frame = CGRectMake(5, 30, 30, 25);
    [backBut setImage:[UIImage imageNamed:@"iconfont-31fanhui1"] forState:UIControlStateNormal];
    [backBut addTarget:self action:@selector(goback) forControlEvents:UIControlEventTouchUpInside];
    [navViea addSubview:backBut];
    [self.view addSubview:navViea];
    
    [navViea release];
}
-(void)goback
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)creatSegment
{
    NSArray *nameArr = @[@"超值自由行",@"精彩当地游"];
    segment = [[UISegmentedControl alloc]initWithItems:nameArr];
    [segment setFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT/20)];
    // 默认选择下标为几的第几个按钮
    [segment setSelectedSegmentIndex:0];
    //设置选中的分段颜色
    [segment setTintColor:[UIColor colorWithRed:34./255 green:169./ 255 blue:118./255 alpha:1]];
    //添加点击事件
    [segment addTarget:self action:@selector(segmentClick:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:segment];
}
- (void)segmentClick:(UISegmentedControl *)seg
{
    if (seg.selectedSegmentIndex == 0)
    {
        self.myPage = 1;
        [self getTheData:seg.selectedSegmentIndex page:1 isUp:NO];
        [self.table setContentOffset:CGPointMake(0, 0)];
    }
    else
    {
        self.myPage = 1;
        [self getTheData:seg.selectedSegmentIndex page:1 isUp:NO];
        [self.table setContentOffset:CGPointMake(0, 0)];
    }
    
}
-(void)getTheData:(NSInteger)number page:(NSInteger)page isUp:(BOOL)up
{
    if (number == 0)
    {
        _finalUrl = [NSString stringWithFormat:@"http://open.qyer.com/qyer/discount/tickets_freewalker?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&id=%@&oauth_token=d5ecb9febb61510d54ba2d47d986cf65&order=2&page=%ld&track_app_version=6.6.6&track_user_id=6695170&type=%d&v=1",self.urlStr,page,self.type];
        
    }else
    {
        _finalUrl = [NSString stringWithFormat:@"http://open.qyer.com/qyer/discount/local_discount?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&id=%@&oauth_token=d5ecb9febb61510d54ba2d47d986cf65&order=2&page=%ld&product_type=2410&time=1&track_app_version=6.6.6&track_user_id=6695170&type=%d&v=1",self.urlStr,page,self.type];
    }

    [AFNblock getdataWithUrlString:_finalUrl dataBlock:^(id block) {
        [self dealDataWithDic:block up:up];
        
    } netBlock:^(id block) {
         [self dealDataWithDic:block up:up];
        
    }];
}
-(void)dealDataWithDic:(NSDictionary *)block up:(BOOL)up
{
    NSDictionary *bigDic = block;
    NSDictionary *dataDic = [bigDic objectForKey:@"data"];
    NSArray *listArr = [dataDic objectForKey:@"list"];
    
    if (!up) {
        
        _inforArr = [[NSMutableArray alloc]init];
    }
    for (int i = 0; i < listArr.count; i++) {
        NSDictionary *dic = [listArr objectAtIndex:i];
        LocalModel *model = [[LocalModel alloc]initWithDictionary:dic];
        [_inforArr addObject:model];
        
    }
    if (_table == nil) {
        
        
        [self creatTableView];
    }
    [_table reloadData];
    
    [_table.footer endRefreshing];
    

}
-(void)creatTableView
{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 64+SCREEN_HEIGHT/20, SCREEN_WIDTH, SCREEN_HEIGHT - 64-SCREEN_HEIGHT/20) style:UITableViewStylePlain];
    [self.table setDelegate:self];
    [self.table setDataSource:self];
    [self.view addSubview:self.table];
    [self shangla];

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _inforArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *wholeFreeCellid = @"wholeFreeCellid";
    FreedomTableViewCell *wholefreeCell = [tableView dequeueReusableCellWithIdentifier:wholeFreeCellid];
    if (!wholefreeCell) {
        wholefreeCell = [[FreedomTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:wholeFreeCellid];
    }
    LocalModel *local = [_inforArr objectAtIndex:indexPath.row];
    [wholefreeCell getvalueWithLacalModel:local];
    return wholefreeCell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return SCREEN_HEIGHT/ 5 + 10;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LocalModel *Lmod = [_inforArr objectAtIndex:indexPath.row];
    SlideViewController *web = [[SlideViewController alloc]init];
    web.url = [NSString stringWithFormat:@"http://m.qyer.com/z/deal/%@/?source=app&client_id=qyer_ios&track_app_version=6.6.6&track_deviceid=301FBCE0-A789-4EAC-ADF1-95E1AACF2A70&track_user_id=6695170",Lmod.cid];
    [self.navigationController pushViewController:web animated:YES];
}
-(void)shangla
{
    __block WholeFreedomViewController *freeC = self;
    [_table addLegendFooterWithRefreshingBlock:^{

        freeC.myPage += 1;
        if (segment.selectedSegmentIndex == 0) {
        
            [freeC getTheData:0 page:freeC.myPage isUp:YES];
        }else{
            [freeC getTheData:1 page:freeC.myPage isUp:YES];
        }
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
